package com.alibaba.shopping_cart_customer_api.controller;

import com.alibaba.shopping_common.pojo.CartGoods;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.CartService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CartController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/18 09:49
 */
@RestController
@RequestMapping("/user/cart")
public class CartController {

    @DubboReference
    private CartService cartService;

    /**
     * 根据ID获取用户购物车数据
     * @param userId
     * @return
     */
    @GetMapping("/findCartList")
    public BaseResult<List<CartGoods>>findCartList(@RequestParam(name = "userId",required = false, defaultValue = "29") Long userId){
        System.out.println("findCartList-userId:"+userId);
        List<CartGoods> cartList = cartService.findCartList(userId);
        return BaseResult.OK(cartList);
    }

    /**
     * 新增购物车数据
     * @param cartGoods
     * @param userId
     * @return
     */
    @PostMapping("/addCart")
    public BaseResult<CartGoods> addCart(
             @RequestBody CartGoods cartGoods,
             @RequestParam(required = false, defaultValue = "29")
             Long userId){
        cartService.addCart(userId,cartGoods);
        return BaseResult.OK(cartGoods);
    }

    /**
     * 修改用户购物车中的数据
     * @param userId
     * @param goodsId
     * @param num
     * @return
     */
    @GetMapping("/handleCart")
    public BaseResult handleCart(@RequestParam(required = false, defaultValue = "29") Long userId,Long goodsId,Integer num){
        cartService.handlerCart(userId, goodsId, num);
        return BaseResult.OK();
    }

    /**
     * 删除用户购物车中的商品数据
     * @param userId
     * @param goodId
     * @return
     */
    @DeleteMapping("/deleteCart")
    public BaseResult deleteCart(@RequestParam(required = false, defaultValue = "29") Long userId,Long goodId){
        cartService.deleteCartOption(userId,goodId) ;
        return BaseResult.OK(); }
}




